﻿using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using CommandBuilder;
using Android.Hardware;
using System.Collections.Generic;
using Common;
using MonoDroid.Utils;

namespace AndroidLinkClient
{
    /// <summary>
    /// Activity1.
    /// </summary>
    [Activity(Label = "AndroidLinkClient", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : TabActivity, ISensorEventListener
    {
        Sensor a_sensor = null;//加速度
        Sensor t_sensor = null;//温度
        Sensor l_sensor = null;//光线
        Sensor pre_sensor = null;//压力
        Sensor pro_sensor = null;//距离
        Sensor g_sensor = null;//回转仪
        Sensor m_sensor = null;//磁场
        Sensor o_sensor = null;//指北针
        SensorManager manager = null;
        Command sensorCommand = new Command();

        /// <summary>
        /// Raises the create event.
        /// </summary>
        /// <param name='bundle'>
        /// Bundle.
        /// </param>
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            TabHost.TabSpec spec;     // Resusable TabSpec for each tab  
            // Create an Intent to launch an Activity for the tab (to be reused)  
            Intent intent = new Intent(this, typeof(Keyboard));
            intent.AddFlags(ActivityFlags.NewTask);

            // Initialize a TabSpec for each tab and add it to the TabHost  
            spec = TabHost.NewTabSpec("键盘");
            spec.SetIndicator("键盘");//, Resources.GetDrawable (Resource.Drawable.Icon));
            spec.SetContent(intent);
            TabHost.AddTab(spec);

            Intent intent1 = new Intent(this, typeof(Mouse));
            intent1.AddFlags(ActivityFlags.NewTask);

            // Initialize a TabSpec for each tab and add it to the TabHost  
            spec = TabHost.NewTabSpec("鼠标");
            spec.SetIndicator("鼠标");//, Resources.GetDrawable (Resource.Drawable.Icon));
            spec.SetContent(intent1);
            TabHost.AddTab(spec);
            Work._default.Connect("192.168.1.21", 6004);
        }

        /// <summary>
        /// Raises the key down event.
        /// </summary>
        /// <param name='keyCode'>
        /// Key code.
        /// </param>
        /// <param name='e'>
        /// E.
        /// </param>
        public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
        {
            if (keyCode == Keycode.Back)
            {
                this.Finish();
            }
            return base.OnKeyDown(keyCode, e);
        }

        /// <summary>
        /// Finish this instance.
        /// </summary>
        public override void Finish()
        {
            Work._default.Close();
            base.Finish();
        }

        /// <summary>
        /// Raises the resume event.
        /// </summary>
        protected override void OnResume()
        {
            base.OnResume();

            // 加速度
            manager = (SensorManager)this.GetSystemService(Context.SensorService);
            a_sensor = manager.GetDefaultSensor(SensorType.Accelerometer);
            t_sensor = manager.GetDefaultSensor(SensorType.Accelerometer);//温度
            l_sensor = manager.GetDefaultSensor(SensorType.Light);//光线
            pre_sensor = manager.GetDefaultSensor(SensorType.Pressure);//压力
            pro_sensor = manager.GetDefaultSensor(SensorType.Proximity);//距离
            g_sensor = manager.GetDefaultSensor(SensorType.Gyroscope);//回转仪
            m_sensor = manager.GetDefaultSensor(SensorType.MagneticField);//磁场
            o_sensor = manager.GetDefaultSensor(SensorType.Orientation);//指北针

            if(a_sensor != null) manager.RegisterListener(this, a_sensor, SensorDelay.Ui);
            //if (t_sensor != null) manager.RegisterListener(this, t_sensor, SensorDelay.Ui);
            //if (l_sensor != null) manager.RegisterListener(this, l_sensor, SensorDelay.Ui);
            //if (pre_sensor != null) manager.RegisterListener(this, pre_sensor, SensorDelay.Ui);
            //if (pro_sensor != null) manager.RegisterListener(this, pro_sensor, SensorDelay.Ui);
            //if (g_sensor != null) manager.RegisterListener(this, g_sensor, SensorDelay.Ui);
            //if (m_sensor != null) manager.RegisterListener(this, m_sensor, SensorDelay.Ui);
            //if (o_sensor != null) manager.RegisterListener(this, o_sensor, SensorDelay.Ui);
        }

        /// <summary>
        /// Raises the accuracy changed event.
        /// </summary>
        /// <param name='sensor'>
        /// Sensor.
        /// </param>
        /// <param name='accuracy'>
        /// Accuracy.
        /// </param>
        public void OnAccuracyChanged(Sensor sensor, SensorStatus accuracy)
        {
            //command.SendMessage(sensor.Power.ToString());
        }

        /// <summary>
        /// Raises the sensor changed event.
        /// </summary>
        /// <param name='e'>
        /// E.
        /// </param>
        public void OnSensorChanged(SensorEvent e)
        {
            sensorCommand.Type = "sensor";
            sensorCommand.Operation = e.Sensor.Name;
            if (e.Values.Count > 0)
                sensorCommand.Commands.Add(new Common.CommandItem() { Name = "x", Value = e.Values[0].ToString() });
            if (e.Values.Count > 1)
                sensorCommand.Commands.Add(new Common.CommandItem() { Name = "y", Value = e.Values[1].ToString() });
            if (e.Values.Count > 2)
                sensorCommand.Commands.Add(new Common.CommandItem() { Name = "z", Value = e.Values[2].ToString() });
            //Work._default.Send(Creator.CreateXml(sensorCommand));
        }

        /// <summary>
        /// 震动
        /// </summary>
        /// <param name='milliSeconds'>
        /// Milli seconds.
        /// </param>
        public void Vibrator(int milliSeconds = 1000)
        {
            Vibrator vib = (Vibrator)this.GetSystemService(Context.VibratorService);
            vib.Vibrate(milliSeconds);
        }
    }
}

